package com.ruoyi.web.invest.stockApply.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.invest.asset.asset.domain.Asset;
import com.ruoyi.web.invest.asset.asset.service.impl.AssetServiceImpl;
import com.ruoyi.web.invest.stockApply.domain.StockApply;
import com.ruoyi.web.invest.stockApply.mapper.StockApplyMapper;
import com.ruoyi.web.invest.stockApply.service.IStockApplyService;
import com.ruoyi.web.operate.stock.domain.Stock;
import com.ruoyi.web.operate.stock.service.impl.StockServiceImpl;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 股票申购委托Service业务层处理
 *
 * @author feng_zi_xiang
 * @date 2021-08-26
 */
@Service
public class StockApplyServiceImpl implements IStockApplyService {
    @Autowired
    private StockApplyMapper stockApplyMapper;
    @Autowired
    private StockServiceImpl stockService;
    @Autowired
    private AssetServiceImpl assetService;
    @Autowired
    private TokenService tokenService;


    /**
     * 查询股票申购委托
     *
     * @param id 股票申购委托主键
     * @return 股票申购委托
     */
    @Override
    public StockApply selectStockApplyById(Long id)
    {
        return stockApplyMapper.selectStockApplyById(id);
    }

    /**
     * 查询股票申购委托列表
     *
     * @param stockApply 股票申购委托
     * @return 股票申购委托
     */
    @Override
    public List<StockApply> selectStockApplyList(StockApply stockApply) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        stockApply.setApplicantId(sysUser.getUserId());
        return stockApplyMapper.selectStockApplyList(stockApply);
    }

    /**
     * 根据购买人ID查询持有人的对应股票
     *
     *
     * @return 资产集合
     */
    @Override
    public List<StockApply> selectDividendStockList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long createById = loginUser.getUserId();
        return stockApplyMapper.selectDividendStockList(createById);
    }

    /**
     * 新增股票申购委托
     *
     * @param stockApply 股票申购委托
     * @return 结果
     */
    @Override
    public int insertStockApply(StockApply stockApply)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        stockApply.setApplicant(sysUser.getNickName());
        stockApply.setApplicantId(sysUser.getUserId());
        stockApply.setTotalMoney(String.valueOf(stockApply.getPrice().multiply(BigDecimal.valueOf(stockApply.getNumber()))));
        stockApply.setStatus("0");
        stockApply.setCreateTime(DateUtils.getNowDate());
        stockApply.setCreateById(sysUser.getUserId());
        int result = stockApplyMapper.insertStockApply(stockApply);
        if (result > 0) {
            Stock stock = stockService.selectStockById(stockApply.getStockId());
            stock.setAmount(stock.getAmount() - stockApply.getNumber());
            stockService.updateStock(stock);
        }
        return result;
    }

    /**
     * 卖出时股票申购委托
     *
     * @param stockApply 股票申购委托
     * @return 结果
     */
    @Override
    public int addStockApply(StockApply stockApply) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        stockApply.setCreateTime(DateUtils.getNowDate());
        stockApply.setCreateById(sysUser.getUserId());
        return stockApplyMapper.insertStockApply(stockApply);
    }

    /**
     * 修改股票申购委托
     *
     * @param stockApply 股票申购委托
     * @return 结果
     */
    @Override
    public int updateStockApply(StockApply stockApply) {
        stockApply.setUpdateTime(DateUtils.getNowDate());
        return stockApplyMapper.updateStockApply(stockApply);
    }

    /**
     * 批量删除股票申购委托
     *
     * @param ids 需要删除的股票申购委托主键
     * @return 结果
     */
    @Override
    public int deleteStockApplyByIds(Long[] ids) {
        Long id = ids[0];
        StockApply stockApply = stockApplyMapper.selectStockApplyById(id);
        Stock stock = stockService.selectStockById(stockApply.getStockId());
        stock.setAmount(stock.getAmount() + stockApply.getNumber());
        stockService.updateStock(stock);

        return stockApplyMapper.deleteStockApplyByIds(ids);
    }

    /**
     * 删除股票申购委托信息
     *
     * @param id 股票申购委托主键
     * @return 结果
     */
    @Override
    public int deleteStockApplyById(Long id) {
        return stockApplyMapper.deleteStockApplyById(id);
    }

    /**
     * 确认股票申购委托
     *
     * @param id 股票申购委托主键
     * @return 结果
     */
    @Override
    public int confirmStockApplyById(Long id) {
        StockApply stockApply = stockApplyMapper.selectStockApplyById(id);
        stockApply.setStatus("1");
        int result = stockApplyMapper.updateStockApply(stockApply);
        if (result > 0) {
            Asset searchAsset = new Asset();
            searchAsset.setStockId(stockApply.getStockId());
            searchAsset.setHolderId(stockApply.getApplicantId());
            List<Asset> assetList = assetService.selectAssetList(searchAsset);

            Stock stock = stockService.selectStockById(stockApply.getStockId());
            stock.setTurnover(stock.getTurnover().add(NumberUtils.createBigDecimal(stockApply.getTotalMoney())));
            stockService.updateStock(stock);

            if (assetList.size() < 1) {
                //没有查到资产，那就新增一条
                searchAsset.setNumber(stockApply.getNumber());
                assetService.insertAsset(searchAsset);
            } else {
                //查到资产，更新资产数量
                Asset asset = assetList.get(0);
                asset.setNumber(asset.getNumber() + stockApply.getNumber());
                assetService.updateAsset(asset);
            }
        }
        return result;
    }
}


























